
let http = require("http");
let fs = require("fs");
let path = require("path");

let server = http.createServer(function (req, res) {
  // console.log(Object.keys(req.url));
  if(req.url == '/'){
    console.log(path.join(__dirname,'index.html'));
    fs.readFile(path.join(__dirname,'index.html'), function (err, data) {
			// res.writeHead(200, { "Content-Type": "text/html" });
			if (data) {
				res.write(data);
			} else {
				res.write("null");
			}
			res.end();
		});
  }else{
    let fileName = path.join(__dirname,req.url)
    var exists = fs.existsSync(fileName);
    if (exists) {
      fs.readFile(fileName, function (err, data) {
        if (data) {
          res.write(data);
        } else {
          res.write("null");
        }
        res.end();
      });
    }
  }
 
});



server.listen(3002, () => {
  console.log("服务器启动成功 ");
});
// 
